package cn.andios.service;

import cn.andios.pojo.Tea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @description:处理登录成功后页面的跳转
 * @author:LSD
 * @when:2019/1/25/12:02
 */
public class LoginSuccessHandle implements AuthenticationSuccessHandler {

    @Autowired
    private TeaService teaService;

    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {

        //得到权限集合
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());

        //得到访问路径
        String path = request.getContextPath() ;
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        //登录用户位stu,就跳转到学生信息页面
        if (roles.contains("ROLE_STU")){
            response.sendRedirect(basePath+"pages/stuInfo/stuInfo.html");
            return;
        }
        //登录用户为tea,就
        String id = SecurityContextHolder.getContext().getAuthentication().getName();
        Tea tea = teaService.findTeaById(id);

        response.sendRedirect(basePath+"pages/teaManage/prototypeCollege.html");
    }

    }

